home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xshisen-.001 / xshisen-~ / xshisen-1.35 / timer.C < prev    next >
C/C++ Source or Header  |  1996-01-07  |  9KB  |  229 lines

  1. #include "components.h"
  2.  
  3. void
  4. Timer::ResetTimer(void)
  5. {
  6.   struct timezone tz;
  7.  
  8.   gettimeofday(&initialtime, &tz);
  9.   timeoffset = 0;
  10.   currenttime = 0;
  11. }
  12.  
  13. long
  14. Timer::GetTimer(void)
  15. {
  16.   struct timeval  tv;
  17.   struct timezone tz;
  18.   long sec, usec;
  19.  
  20.   gettimeofday(&tv, &tz);
  21.   sec = tv.tv_sec - initialtime.tv_sec;
  22.   if (tv.tv_usec < initialtime.tv_usec)
  23.     usec = 1000000;
  24.   else
  25.     usec = 0;
  26.   usec += tv.tv_usec - initialtime.tv_usec;
  27.   return (timeoffset+sec*1000+usec/1000);
  28. }
  29.  
  30. void
  31. Timer::SetTimer(long tim)
  32. {
  33.   struct timezone tz;
  34.  
  35.   gettimeofday(&initialtime, &tz);
  36.   timeoffset = tim;
  37.   currenttime = 0;
  38. }
  39.  
  40. TimerW::TimerW(Widget parent, Widget top, char *s1, char *s2,
  41.                char *s3, char *s4)
  42. {
  43. #if USE_MOTIF
  44.     XmString ms;
  45.  
  46.     ms = XmStringCreateLocalized(s4);
  47.     label[3] = XtVaCreateManagedWidget("label3", xmLabelWidgetClass, parent,
  48.                                        XmNtopAttachment,    XmATTACH_WIDGET,
  49.                                        XmNrightAttachment,  XmATTACH_FORM,
  50.                                        XmNleftAttachment,   XmATTACH_NONE,
  51.                                        XmNbottomAttachment, XmATTACH_NONE,
  52.                                        XmNalignment,        XmALIGNMENT_END,
  53.                                        XmNlabelString,      ms,
  54.                                        XmNtopWidget,        top,
  55.                                        XmNrecomputeSize,    False,
  56.                                        NULL);
  57.     XmStringFree(ms);
  58.  
  59.     ms = XmStringCreateLocalized(s3);
  60.     label[2] = XtVaCreateManagedWidget("label2", xmLabelWidgetClass, parent,
  61.                                        XmNtopAttachment,    XmATTACH_WIDGET,
  62.                                        XmNrightAttachment,  XmATTACH_WIDGET,
  63.                                        XmNleftAttachment,   XmATTACH_NONE,
  64.                                        XmNbottomAttachment, XmATTACH_NONE,
  65.                                        XmNalignment,        XmALIGNMENT_END,
  66.                                        XmNlabelString,      ms,
  67.                                        XmNtopWidget,        top,
  68.                                        XmNrightWidget,      label[3],
  69.                                        XmNmarginLeft,       12,
  70.                                        XmNrecomputeSize,    False,
  71.                                        NULL);
  72.     XmStringFree(ms);
  73.  
  74.     ms = XmStringCreateLocalized(s2);
  75.     label[1] = XtVaCreateManagedWidget("label1", xmLabelWidgetClass, parent,
  76.                                        XmNtopAttachment,    XmATTACH_WIDGET,
  77.                                        XmNrightAttachment,  XmATTACH_WIDGET,
  78.                                        XmNleftAttachment,   XmATTACH_NONE,
  79.                                        XmNbottomAttachment, XmATTACH_NONE,
  80.                                        XmNalignment,        XmALIGNMENT_END,
  81.                                        XmNlabelString,      ms,
  82.                                        XmNtopWidget,        top,
  83.                                        XmNrightWidget,      label[2],
  84.                                        XmNrecomputeSize,    False,
  85.                                        NULL);
  86.     XmStringFree(ms);
  87.  
  88.     str1 = XmStringCreateLocalized(s1);
  89.     label[0] = XtVaCreateManagedWidget("label0", xmLabelWidgetClass, parent,
  90.                                        XmNtopAttachment,    XmATTACH_WIDGET,
  91.                                        XmNrightAttachment,  XmATTACH_WIDGET,
  92.                                        XmNleftAttachment,   XmATTACH_FORM,
  93.                                        XmNbottomAttachment, XmATTACH_NONE,
  94.                                        XmNalignment,        XmALIGNMENT_END,
  95.                                        XmNlabelString,      str1,
  96.                                        XmNtopWidget,        top,
  97.                                        XmNrightWidget,      label[1],
  98.                                        XmNrecomputeSize,    True,
  99.                                        NULL);
  100.  
  101. #else /* USE_MOTIF */
  102.     /* Don't use "top" with Athena Widget */
  103.     label[0] = XtVaCreateManagedWidget("label0", labelWidgetClass, parent,
  104.                                        XtNtop,         XawChainTop,
  105.                                        XtNbottom,      XawChainTop,
  106.                                        XtNleft,        XawChainLeft,
  107.                                        XtNright,       XawChainLeft,
  108.                                        XtNresizable,   True,
  109.                                        XtNborderWidth, 0,
  110.                                        XtNlabel,       s1,
  111.                                        NULL);
  112.     label[1] = XtVaCreateManagedWidget("label1", labelWidgetClass, parent,
  113.                                        XtNfromHoriz,   label[0],
  114.                                        XtNtop,         XawChainTop,
  115.                                        XtNbottom,      XawChainTop,
  116.                                        XtNleft,        XawChainLeft,
  117.                                        XtNright,       XawChainLeft,
  118.                                        XtNborderWidth, 0,
  119.                                        XtNlabel,       s2,
  120.                                        NULL);
  121.     label[2] = XtVaCreateManagedWidget("label2", labelWidgetClass, parent,
  122.                                        XtNfromHoriz,   label[1],
  123.                                        XtNtop,         XawChainTop,
  124.                                        XtNbottom,      XawChainTop,
  125.                                        XtNleft,        XawChainLeft,
  126.                                        XtNright,       XawChainLeft,
  127.                                        XtNborderWidth, 0,
  128.                                        XtNlabel,       s3,
  129.                                        NULL);
  130.     label[3] = XtVaCreateManagedWidget("label3", labelWidgetClass, parent,
  131.                                        XtNfromHoriz,   label[2],
  132.                                        XtNtop,         XawChainTop,
  133.                                        XtNbottom,      XawChainTop,
  134.                                        XtNleft,        XawChainLeft,
  135.                                        XtNright,       XawChainLeft,
  136.                                        XtNborderWidth, 0,
  137.                                        XtNlabel,       s4,
  138.                                        NULL);
  139.     str1 = s1;
  140. #endif /* USE_MOTIF */
  141.     dateMode = 0;
  142.     countNow = 0;
  143.     ResetTimer();
  144. }
  145.  
  146. void
  147. TimerW::DisplayTimer(int count)
  148. {
  149.     char buffer[128];
  150.     long t;
  151.  
  152.     if (dateMode) {
  153.         XtManageChild(label[1]);
  154.         XtManageChild(label[2]);
  155.         XtManageChild(label[3]);
  156.         dateMode = 0;
  157. #if USE_MOTIF
  158.         XtVaSetValues(label[0],
  159.                       XmNlabelString,      str1,
  160.                       XmNrightAttachment,  XmATTACH_WIDGET,
  161.                       XmNrightWidget,      label[1],
  162.                       NULL);
  163. #else /* USE_MOTIF */
  164.         XtVaSetValues(label[0],
  165.                       XtNlabel, str1,
  166.                       NULL);
  167. #endif /* USE_MOTIF */
  168.     }
  169.     if (count != countNow) {
  170.         sprintf(buffer, "%3.3d", count);
  171. #if USE_MOTIF
  172.         XmString s = XmStringCreateLocalized(buffer);
  173.         XtVaSetValues(label[1], XmNlabelString, s, NULL);
  174.         XmStringFree(s);
  175. #else /* USE_MOTIF */
  176.         XtVaSetValues(label[1], XtNlabel, buffer, NULL);
  177. #endif /* USE_MOTIF */
  178.         countNow = count;
  179.     }
  180.     t = GetTimer() / 1000;
  181.     if (t > currenttime) {
  182.         sprintf(buffer, "%2.2d:%2.2d:%2.2d", t/3600, (t/60)%60, t%60);
  183. #if USE_MOTIF
  184.         XmString s = XmStringCreateLocalized(buffer);
  185.         XtVaSetValues(label[3], XmNlabelString, s, NULL);
  186.         XmStringFree(s);
  187. #else /* USE_MOTIF */
  188.         XtVaSetValues(label[3], XtNlabel, buffer, NULL);
  189. #endif /* USE_MOTIF */
  190.         currenttime = t;
  191.     }
  192. }
  193.  
  194. void
  195. TimerW::DisplayCurrentTime(void)
  196. {
  197.     time_t   t;
  198.     static time_t t_before = 0;
  199.     char     *timestr, *p;
  200.  
  201.     if (!dateMode) {
  202.         XtUnmanageChild(label[1]);
  203.         XtUnmanageChild(label[2]);
  204.         XtUnmanageChild(label[3]);
  205.         dateMode = 1;
  206. #if USE_MOTIF
  207.         XtVaSetValues(label[0],
  208.                       XmNrightAttachment,  XmATTACH_FORM,
  209.                       NULL);
  210. #endif /* USE_MOTIF */
  211.     }
  212.     t = time(NULL);
  213.     if (t == t_before)
  214.         return;
  215.     t_before = t;
  216.     timestr = ctime(&t);
  217.     if ((p = strchr(timestr, '\n')) != NULL)
  218.         *p = '\0';
  219. #if USE_MOTIF
  220.     XmString s = XmStringCreateLocalized(timestr);
  221.     XtVaSetValues(label[0],
  222.                   XmNlabelString, s,
  223.                   NULL);
  224.     XmStringFree(s);
  225. #else /* USE_MOTIF */
  226.     XtVaSetValues(label[0], XtNlabel, timestr, NULL);
  227. #endif /* USE_MOTIF */
  228. }
  229.